/**!
 *
 * 添加通知。
 *
 */

$(function () {
    /** 通知表单的 jQuery 对象 */
    var $noticeForm = $("#notice_form");
    /** 通知附件名称的 jQuery 对象 */
    var $noticeFileNames = $noticeForm.find("input[name='noticeFileNames']");
    /** 通知附件路径的 jQuery 对象 */
    var $noticeFilePaths = $noticeForm.find("input[name='noticeFilePaths']");
    /** 上传通知附件的 jQuery 对象 */
    var $uploadNoticeFilesBtn = $("#upload_notice_files_btn");
    /** 显示校验信息的容器 */
    var $validatecontainer = $("#validate_container");
    /** 添加按钮的 jQuery 对象 */
    var $addNoticeBtn = $noticeForm.find("button[name='addNoticeBtn']");
    /** 通知附件的名称 */
    var noticeFileNames = "";
    /** 通知附件的路径 */
    var noticeFilePaths = "";

    // 上传附件
    var $noticeFilesUploadifive = $uploadNoticeFilesBtn.myUploadifive({
        auto: false,
        multi: true,
        width: 80,
        height: 30,
        queueID: "notice_queue",
        buttonText: "<i class='ace-icon iconfont icon-upload f-s-12'></i>上传附件",
        buttonClass: "btn btn-success btn-xs",
        fileObjName: "file",
        uploadScript: "{0}web/file/upload.json".format(globalBasePath),
        fileSizeLimit: String(50 * 1024),
        onUploadFn: function () {
            $noticeFilesUploadifive.uploadifiveData.settings.formData.path = "/upload/notice";
        },
        onUploadCompleteFn: function (file, data) {
            var result = data.result;

            noticeFileNames += (result.fileName + "|");
            noticeFilePaths += (result.filePath + "|");
        },
        onQueueCompleteFn: function () {
            $noticeFileNames.val(noticeFileNames.substring(0, (noticeFileNames.length - 1)));
            $noticeFilePaths.val(noticeFilePaths.substring(0, (noticeFilePaths.length - 1)));

            // 提交表单
            $noticeForm.get(0).submit();
        }
    });

    $noticeForm.validate({
        rules: {
            noticeName: {
                required: true,
                maxlength: 500
            },
            noticeContent: {
                required: true,
                maxlength: 2000
            },
            noticeLevel: {
                required: true
            }
        },
        messages: {
            noticeName: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入通知标题</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>通知标题的长度不能大于{0}个字符</span>"
            },
            noticeContent: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入通知内容</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>通知内容的长度不能大于{0}个字</span>"
            },
            noticeLevel: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请选择通知级别</span>"
            }
        },
        errorContainer: $validatecontainer,
        errorLabelContainer: $("ul", $validatecontainer),
        wrapper: "li",
        submitHandler: function (form) {
            $addNoticeBtn.html("<i class='ace-icon fa fa-spinner fa-spin bigger-150'></i>正在提交…").attr({"disabled": "disabled"});

            if ($.isNotNull($noticeFilesUploadifive) && ($noticeFilesUploadifive.uploadifiveData.queue.count > 0)) {
                // 开始上传文件
                $uploadNoticeFilesBtn.uploadifive("upload");
            } else {
                // 提交表单
                $noticeForm.get(0).submit();
            }

            return false;
        }
    });
});